package com.tsingteng.cosfun.ui.cosfun.adapter;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.pili.pldroid.player.AVOptions;
import com.pili.pldroid.player.IMediaController;
import com.pili.pldroid.player.PLOnErrorListener;
import com.pili.pldroid.player.PLOnInfoListener;
import com.pili.pldroid.player.PlayerState;
import com.pili.pldroid.player.widget.PLVideoTextureView;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.EventBean;
import com.tsingteng.cosfun.bean.HomeBean;
import com.tsingteng.cosfun.bean.OpusBean;
import com.tsingteng.cosfun.callback.AppListener;
import com.tsingteng.cosfun.helper.DialogHelper;
import com.tsingteng.cosfun.ui.cosfun.PlayActivity;
import com.tsingteng.cosfun.ui.mine.HomeMineFragment;
import com.tsingteng.cosfun.ui.mine.TerminalActivity;
import com.tsingteng.cosfun.utils.AnimatorUtils;
import com.tsingteng.cosfun.utils.AppUtils;
import com.tsingteng.cosfun.utils.ImageUtils;
import com.tsingteng.cosfun.utils.LogUtils;
import com.tsingteng.cosfun.utils.LogonUtils;
import com.tsingteng.cosfun.utils.NotchUtil;
import com.tsingteng.cosfun.utils.StringUtils;
import com.tsingteng.cosfun.utils.VideoUtils;
import com.tsingteng.cosfun.widget.HeartView;

import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import de.hdodenhof.circleimageview.CircleImageView;

public class OpusAdapterNew extends RecyclerView.Adapter {
    String TAG = "opusadapternew";

    public enum FooterState {
        Normal, Loading, TheEnd;
    }

    private long pageType = 0;//1烤饭 2关注 大于3公共播放界面

    public static final String EVENT_PAUSE = "EVENT_PAUSE";
    public static final String EVENT_START = "EVENT_START";
    private final int FOOTERLAYOUT = 100;//加载更多
    public FooterHolder mFooterHolder;
    public ViewHolder playViewHolder = null;
    private Activity mContext = null;
    private List<OpusBean> videoList = null;

    private List<String> url = new ArrayList<>();
    private ArrayList<String> urlPath = new ArrayList<>();
    private final Dialog loadingDialog;
    int h = 0;
    int w = 0;
    private boolean hasPip = false;
    boolean sharkFlag = true;// 摇一摇

    private OnItemClickLisenter onItemClickLisenter = null;
    private ActivityClickListener mActivityClickListener;

    IMediaController.MediaPlayerControl playerControl = null;
    int currentPosition = 0;
    private Map<String, ViewHolder> videoPlayer; //缓存本地播放器
    private List<String> videoIds; //缓存本地播放器

    //双击点赞动画
    private long mCurrentime;
    private boolean isDoubleClick = false;
    private int touchX;
    private int touchY;
    int mTrand =0;
    private View.OnTouchListener listener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            touchX = (int) event.getX();
            touchY = (int) event.getY();
            LogUtils.i("videoxy", touchX + "dsfdf" + touchY);
            return false;
        }
    };

    public OpusAdapterNew(Activity mContext, long pageType, List<OpusBean> videoList, int currentPosition, OnItemClickLisenter onItemClickLisenter) {
        this.mContext = mContext;
        this.videoList = videoList;
        this.pageType = pageType;
        loadingDialog = DialogHelper.getLoadingDialog(mContext);
        w = AppUtils.getScreenWidthAndHeight(mContext)[0];
        h = AppUtils.getScreenWidthAndHeight(mContext)[1];
        this.onItemClickLisenter = onItemClickLisenter;
        this.currentPosition = currentPosition;
        videoPlayer = new HashMap<String, ViewHolder>();//缓存本地播放器
        videoIds = new ArrayList<>();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == 0) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.opus_item_view, parent, false);
//            playViewHolder = new OpusAdapterNew.ViewHolder(view);
            return new OpusAdapterNew.ViewHolder(view);
        } else {//加载更多
            view = LayoutInflater.from(mContext).inflate(R.layout.listview_footer, parent, false);
            mFooterHolder = new FooterHolder(view);
            return mFooterHolder;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof OpusAdapterNew.ViewHolder) {
            ((OpusAdapterNew.ViewHolder) holder).setData(position);
            addPlayer(position, ((OpusAdapterNew.ViewHolder) holder));
        }
    }


    @Override
    public int getItemViewType(int position) {
        if (getItemCount() > 0 && position == getItemCount() - 1) {
            return FOOTERLAYOUT;
        } else {
            return 0;
        }
    }

    @Override
    public int getItemCount() {
        if (videoList == null) {
            return 0;
        }
        return videoList.size() + 1;
    }


    public boolean isHasPip() {
        return hasPip;
    }

    public void setHasPip(boolean hasPip) {
        this.hasPip = hasPip;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        PLVideoTextureView videoMian;
        ImageView video_cover;
        PLVideoTextureView videoSecond;
        ImageView img_play;
        RelativeLayout rootView;
        LinearLayout lin_likeView;
        LinearLayout lin_comment;
        LinearLayout lin_share;
        TextView likeView;
        TextView tv_comment;
        TextView tv_share;
        ImageView like_img;
        TextView tv_des;
        TextView mActivityTv;
        TextView mCountTv;
        RelativeLayout rl_awkward;
        LinearLayout lin2;
        CircleImageView user_img;
        ImageView add_img;
        CircleImageView user_join_img;
        TextView user_join_tes;
        ImageView join_img;
        RelativeLayout join_rel;
        LinearLayout mJoinLl;
        ImageView isAttentionCostar;
        TextView mCommendTv;
        TextView mCheckTv;

        HeartView heartView;
        int currentSelect = 0;
        int trand = 0;

        public ViewHolder(View itemView) {
            super(itemView);
            mCheckTv = itemView.findViewById(R.id.check_tv);
            mCommendTv = itemView.findViewById(R.id.commend_tv);
            mJoinLl = itemView.findViewById(R.id.join_ll);
            video_cover = itemView.findViewById(R.id.opus_item_view_video_cover);
            videoMian = itemView.findViewById(R.id.opus_item_view_video_main);
            videoSecond = itemView.findViewById(R.id.opus_item_view_video_second);
            img_play = itemView.findViewById(R.id.opus_item_view_play);
            rootView = itemView.findViewById(R.id.opus_item_view_root_view);
            likeView = itemView.findViewById(R.id.opus_item_view_like);
            tv_comment = itemView.findViewById(R.id.opus_item_view_comment);
            tv_share = itemView.findViewById(R.id.opus_item_view_share);
            like_img = itemView.findViewById(R.id.opus_item_view_like_img);
            lin_likeView = itemView.findViewById(R.id.opus_item_view_like_lin);
            lin_comment = itemView.findViewById(R.id.opus_item_view_comment_lin);
            lin_share = itemView.findViewById(R.id.opus_item_view_share_lin);

            rl_awkward = itemView.findViewById(R.id.opus_item_view_awkward_rel);
            tv_des = itemView.findViewById(R.id.opus_item_view_des);
            mActivityTv = itemView.findViewById(R.id.activity_tv);
            mCountTv = itemView.findViewById(R.id.count_tv);
            lin2 = itemView.findViewById(R.id.opus_item_view_video_lin2);
//            img1 = itemView.findViewById(R.id.opus_item_view_awkward_img1);
//            img2 = itemView.findViewById(R.id.opus_item_view_awkward_img2);
//            img3 = itemView.findViewById(R.id.opus_item_view_awkward_img3);
//            heartView = itemView.findViewById(R.id.opus_item_view_video_heartView);
            user_img = itemView.findViewById(R.id.opus_item_view_user_img1);
            add_img = itemView.findViewById(R.id.opus_item_view_user_guanzhu);
            user_join_img = itemView.findViewById(R.id.opus_item_view_user_join);
            user_join_tes = itemView.findViewById(R.id.opus_item_view_user_join_des);
            join_img = itemView.findViewById(R.id.opus_item_view_join);
            join_rel = itemView.findViewById(R.id.opus_item_view_user_join_rel);
            isAttentionCostar = itemView.findViewById(R.id.opus_item_view_user_joi_guanzhu);
//            EventBus.getDefault().register(this);
        }

        @Subscriber(tag = OpusAdapterNew.EVENT_PAUSE)
        void playerPause(EventBean eventBean) {
            if (videoMian != null && pageType == eventBean.getType() && PlayerState.PLAYING_CACHE == videoMian.getPlayerState()) {
                videoMian.pause();
            }
        }


        @Subscriber(tag = OpusAdapterNew.EVENT_START)
        void playerStart(EventBean eventBean) {
            if (eventBean.getPosition() == currentSelect && videoMian != null && pageType == eventBean.getType()) {
                videoMian.setMediaController(iMediaController);
                final OpusBean opusBean = videoList.get(currentSelect);
                if (!videoMian.isPlaying()) {
                    videoMian.setVideoPath(opusBean.getVideoUrl());
                    videoMian.start();
                }
                img_play.setVisibility(View.INVISIBLE);
            }
        }

        public void setData(int position) {
            trand = 0;
            currentSelect = position;
            final OpusBean opusBean = videoList.get(position);
//            videoSecond.setDisplayAspectRatio(PLVideoTextureView.ASPECT_RATIO_PAVED_PARENT);
            if (opusBean.getWidth() == 0 || opusBean.getHeight() == 0) {
                videoMian.setDisplayAspectRatio(PLVideoTextureView.ASPECT_RATIO_PAVED_PARENT);//铺满屏幕
                ImageUtils.LoadImagePlay(mContext,opusBean.getVideoCverUrl(),video_cover);
                videoMian.setCoverView(video_cover);
            } else if (opusBean.getWidth() > opusBean.getHeight()) {//宽度大于高度
                videoMian.setDisplayAspectRatio(PLVideoTextureView.ASPECT_RATIO_FIT_PARENT);//适应屏幕
                ImageUtils.LoadImagePlay(mContext,"",video_cover);
                videoMian.setCoverView(video_cover);
            } else {
                videoMian.setDisplayAspectRatio(PLVideoTextureView.ASPECT_RATIO_PAVED_PARENT);//铺满屏幕
                ImageUtils.LoadImagePlay(mContext,opusBean.getVideoCverUrl(),video_cover);
                videoMian.setCoverView(video_cover);
            }
            videoMian.setOnInfoListener(mOnInfoListener1);
            videoMian.setOnErrorListener(mOnErrorListener1);
            videoMian.setMediaController(iMediaController);
            videoMian.setLooping(true);
            video_cover.setFitsSystemWindows(true);
            videoMian.setVideoPath(opusBean.getVideoUrl());
            img_play.setVisibility(View.INVISIBLE);

            rootView.setOnTouchListener(listener);
            rootView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (null != opusBean) {
                        onItemClickLisenter.longPress(position, opusBean);
                    }
                    return true;
                }
            });

            rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (System.currentTimeMillis() - mCurrentime < 500) {
                        isDoubleClick = true;
                        LogUtils.i("videoxy", touchX + "gusheng" + touchY);
                        if (null != opusBean) {
                            onItemClickLisenter.zan(1, position, opusBean);
                        }
                        AnimatorUtils.getIntecnce().doublePraseAnimtor(rootView, touchX, touchY);
                    } else {
                        isDoubleClick = false;
                        mCurrentime = System.currentTimeMillis();
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                if (isDoubleClick) return;
                                if (videoMian != null) {
                                    if (videoMian.isPlaying()) {
                                        videoMian.pause();
                                        img_play.setVisibility(View.VISIBLE);
                                    } else {
                                        videoMian.setMediaController(iMediaController);
                                        videoMian.start();
                                        img_play.setVisibility(View.INVISIBLE);
                                    }

                                }
                            }
                        }, 500);
                    }
                }
            });
            img_play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (videoMian != null) {
                        videoMian.start();
                        img_play.setVisibility(View.INVISIBLE);
                        onItemClickLisenter.startPlay();
                    }
                }
            });
            if (!TextUtils.isEmpty(opusBean.getCostarVideoInfoUrl()) && AppUtils.isHuaZhongHua)

            {
                videoSecond.setVideoPath(opusBean.getCostarVideoInfoUrl());
                lin2.setVisibility(View.VISIBLE);
                setHasPip(true);
            } else

            {
                setHasPip(false);
                lin2.setVisibility(View.INVISIBLE);
            }
//            if (foundIs) {
            ImageUtils.LoadImage(mContext, opusBean.getProfileAvatar(), user_img);
//                foundIs = false;
//            }

            if (opusBean.getCostarProfileId() == 0) {
                join_rel.setVisibility(View.GONE);
            } else

            {
                join_rel.setVisibility(View.VISIBLE);
            }

            likeView.setText(StringUtils.formatZanCount(opusBean.getLikeCount()));
            tv_comment.setText(StringUtils.formatZanCount(opusBean.getCommentCount()));
            trand = opusBean.getTranspondCount();
            tv_share.setText(StringUtils.formatZanCount(trand));
            if (TextUtils.isEmpty(opusBean.getVideoDesp())) {
                tv_des.setVisibility(View.GONE);
            } else {
                tv_des.setText(opusBean.getVideoDesp());
                tv_des.setVisibility(View.VISIBLE);
            }
            if (TextUtils.isEmpty(opusBean.getActivityTitle())) {
                mActivityTv.setVisibility(View.GONE);
            } else {
                String title = mContext.getResources().getString(R.string.activity_title) + opusBean.getActivityTitle();
                mActivityTv.setText(title);
                mActivityTv.setVisibility(View.VISIBLE);
            }
            if ("1".equals(opusBean.getIsActivity())) {
                join_img.setBackgroundResource(R.drawable.and_play_activity);
            } else {
                join_img.setBackgroundResource(R.drawable.and_play);
            }
            mActivityTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (null != opusBean) {
                        mActivityClickListener.ActivityClick(opusBean);
                    }
                }
            });
            if (0 != opusBean.getCostarCount()) {
                mCountTv.setVisibility(View.VISIBLE);
                mCountTv.setText(String.valueOf(opusBean.getCostarCount()));
            } else {
                mCountTv.setVisibility(View.INVISIBLE);
            }
//            img1.setVisibility(View.INVISIBLE);
//            img2.setVisibility(View.INVISIBLE);
//            img3.setVisibility(View.INVISIBLE);
            if (sharkFlag) {
                likeView.setVisibility(View.VISIBLE);
                tv_comment.setVisibility(View.VISIBLE);
                tv_share.setVisibility(View.VISIBLE);
                rl_awkward.setVisibility(View.VISIBLE);
            } else {
                likeView.setVisibility(View.GONE);
                tv_comment.setVisibility(View.GONE);
                tv_share.setVisibility(View.GONE);
                rl_awkward.setVisibility(View.INVISIBLE);
            }


            if (opusBean.getVideoBattleCommentList() != null && opusBean.getVideoBattleCommentList().
                    size() > 0) {
                int count = 0;
                if (opusBean.getVideoBattleCommentList().size() <= 3) {
                    count = opusBean.getVideoBattleCommentList().size();
                } else {
                    count = 3;
                }
                for (int i = 0; i < count; i++) {
                    if (i == 0) {
//                        Glide.with(mContext).load(opusBean.getVideoBattleCommentList().get(i).getBattleProfileAvatar()).into(img1);
//                        img1.setVisibility(View.VISIBLE);
                    }
                    if (i == 1) {
//                        Glide.with(mContext).load(opusBean.getVideoBattleCommentList().get(i).getBattleProfileAvatar()).into(img2);
//                        img2.setVisibility(View.VISIBLE);
                    }
                    if (i == 2) {
//                        Glide.with(mContext).load(opusBean.getVideoBattleCommentList().get(i).getBattleProfileAvatar()).into(img3);
//                        img3.setVisibility(View.VISIBLE);
                    }
                }
            } else

            {
//                img1.setVisibility(View.VISIBLE);
            }
            if (opusBean.getIsLike() == 1 && LogonUtils.getIsLogin())

            {
                like_img.setImageDrawable(mContext.getResources().getDrawable(R.drawable.great_pink_icon));
            } else

            {
                like_img.setImageDrawable(mContext.getResources().getDrawable(R.drawable.great_icon));

            }

            if (position == currentSelect)

            {
                if (onItemClickLisenter != null) {
//                    onItemClickLisenter.playFirst(position, videoMian, videoSecond);
                }
            }
            if (opusBean.getIsAttention() == 1 || opusBean.getProfileId() == LogonUtils.getProFildId())

            {
                add_img.setVisibility(View.GONE);
            } else

            {
                add_img.setVisibility(View.VISIBLE);
            }
            add_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickLisenter != null) {
                        if (null != opusBean) {
                            if (!LogonUtils.chckLogon(mContext, false, 4)) return;
                            onItemClickLisenter.guanzhu(position, opusBean);
                            opusBean.setIsAttention(1);
                            VideoUtils.handelUserGuznZhu(opusBean.getProfileId() + "", "1");
                            if (null ==opusBean.getVideoSectionInfoList()|| 0 ==opusBean.getVideoSectionInfoList().size()){
                                return;
                            }
                            for (int i = 0;i<opusBean.getVideoSectionInfoList().size();i++){
                                if (opusBean.getProfileId() == opusBean.getVideoSectionInfoList().get(i).getCostarProfileId()){
                                    opusBean.getVideoSectionInfoList().get(i).setIsAttentionCostar(1);
                                }
                            }
                        }
                    }
                }
            });
            user_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != opusBean) {
                        Bundle bundle = new Bundle();
                        bundle.putInt("user_id", opusBean.getProfileId());
                        bundle.putInt("tag", 1);
                        TerminalActivity.showFragment(mContext, HomeMineFragment.class, bundle);
                    }
                }
            });
            isAttentionCostar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickLisenter != null) {
                        if (null != opusBean) {
                            if (!LogonUtils.chckLogon(mContext, false, 4)) return;
                            onItemClickLisenter.guanzhu(position, opusBean);
                        }
                    }
                }
            });

            rl_awkward.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickLisenter != null) {
                        if (null != opusBean) {
                            if (!LogonUtils.chckLogon(mContext, false, 4)) return;
                            onItemClickLisenter.awkward(position, opusBean);
                        }
                    }
                }
            });

            lin_likeView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!LogonUtils.chckLogon(mContext, false, 4)) return;
                    if (null != opusBean) {
                        if (opusBean.getIsLike() == 1) {
                            if (onItemClickLisenter != null) {
                                onItemClickLisenter.zan(0, position, opusBean);
                                VideoUtils.handelOpusLike(opusBean.getId() + "'", "0");
                            }
                        } else {
                            if (onItemClickLisenter != null) {
                                onItemClickLisenter.zan(1, position, opusBean);
                                VideoUtils.handelOpusLike(opusBean.getId() + "'", "1");
                            }

                        }
                    }

                }
            });
            lin_comment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickLisenter != null) {
                        if (null != opusBean) {
                            onItemClickLisenter.comm(position, opusBean);
                        }
                    }
                }
            });
            lin_share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickLisenter != null) {
                        if (null != opusBean) {
                            onItemClickLisenter.share(position, opusBean);
                        }
                    }
                }
            });
            lin2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext, "点击了画中画", Toast.LENGTH_LONG).show();
                    doOpenAnimator(lin2);
                }
            });
            join_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickLisenter != null) {
                        if (null != opusBean) {
                            onItemClickLisenter.join(position, opusBean);
                        }
                    }
                }
            });
            mCommendTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (null != onItemClickLisenter){
                        if (null != opusBean){
                            onItemClickLisenter.remmend(position,opusBean);
                        }
                    }
                }
            });
            mCheckTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (null != onItemClickLisenter){
                        if (null != opusBean){
                            onItemClickLisenter.checkAndPass(position,opusBean);
                        }
                    }
                }
            });
            if (0==LogonUtils.getIsAdmin()){
                mCommendTv.setVisibility(View.GONE);
                mCheckTv.setVisibility(View.GONE);
                lin_likeView.setVisibility(View.VISIBLE);
                lin_comment.setVisibility(View.VISIBLE);
                lin_share.setVisibility(View.VISIBLE);
                mJoinLl.setVisibility(View.VISIBLE);
            }else if (1==LogonUtils.getIsAdmin()) {
                mCommendTv.setVisibility(View.VISIBLE);
                mCheckTv.setVisibility(View.VISIBLE);
                lin_likeView.setVisibility(View.GONE);
                lin_comment.setVisibility(View.GONE);
                lin_share.setVisibility(View.GONE);
                mJoinLl.setVisibility(View.GONE);
            }
        }

    }

    private PLOnInfoListener mOnInfoListener1 = new PLOnInfoListener() {
        @Override
        public void onInfo(int what, int extra) {
            Log.v(TAG, "OnInfo, what = " + what + ", extra = " + extra);
            switch (what) {
                case PLOnInfoListener.MEDIA_INFO_BUFFERING_START://开始缓冲
                    break;
                case PLOnInfoListener.MEDIA_INFO_BUFFERING_END://停止缓冲
                    break;
                case PLOnInfoListener.MEDIA_INFO_VIDEO_RENDERING_START://第一帧视频已成功渲染
                    break;
                case PLOnInfoListener.MEDIA_INFO_AUDIO_RENDERING_START://第一帧音频已成功播放
                    if (onItemClickLisenter != null) {
                        onItemClickLisenter.showPlayProgress(0, playerControl);
                    }
//                        mHandler.sendEmptyMessageDelayed(SHOW_PROGRESS, 100);
                    break;
                case PLOnInfoListener.MEDIA_INFO_VIDEO_FRAME_RENDERING:
                    Log.v(TAG, "video frame rendering, ts = " + extra);
                    break;
                case PLOnInfoListener.MEDIA_INFO_AUDIO_FRAME_RENDERING://音频帧的时间戳
                    Log.v(TAG, "audio frame rendering, ts = " + extra);
                    break;
                case PLOnInfoListener.MEDIA_INFO_VIDEO_GOP_TIME:
                    Log.v(TAG, "Gop Time: " + extra);
                    break;
                case PLOnInfoListener.MEDIA_INFO_SWITCHING_SW_DECODE:
                    Log.v(TAG, "Hardware decoding failure, switching software decoding!");
                    break;
                case PLOnInfoListener.MEDIA_INFO_METADATA:
                    break;
                case PLOnInfoListener.MEDIA_INFO_VIDEO_BITRATE:
                case PLOnInfoListener.MEDIA_INFO_VIDEO_FPS:
//                    updateStatInfo();
                    break;
                case PLOnInfoListener.MEDIA_INFO_CONNECTED:
                    Log.v(TAG, "Connected !");
                    break;
                case PLOnInfoListener.MEDIA_INFO_LOOP_DONE:// loop 中的一次播放完成
                    if (onItemClickLisenter != null) {
                        onItemClickLisenter.playSuccess(0);
                    }
                    break;
                case PLOnInfoListener.MEDIA_INFO_VIDEO_ROTATION_CHANGED:
                    Log.v(TAG, "Rotation changed: " + extra);
                default:


                    break;
            }
        }
    };
    private PLOnErrorListener mOnErrorListener1 = new PLOnErrorListener() {
        @Override
        public boolean onError(int errorCode) {
            Log.e(TAG, "Error happened, errorCode = " + errorCode);
            switch (errorCode) {
                case PLOnErrorListener.ERROR_CODE_IO_ERROR:
                    /**
                     * SDK will do reconnecting automatically
                     */
                    Log.e(TAG, "IO Error!");
                    return false;
                case PLOnErrorListener.ERROR_CODE_OPEN_FAILED:
                    Log.i(TAG, "failed to open player !");
                    break;
                case PLOnErrorListener.ERROR_CODE_SEEK_FAILED:
                    Log.i(TAG, "failed to seek !");
                    break;
                default:
                    Log.i(TAG, "unknown error !");
                    break;
            }
            return true;
        }
    };

    private IMediaController iMediaController = new IMediaController() {
        @Override
        public void setMediaPlayer(MediaPlayerControl mediaPlayerControl) {
            playerControl = mediaPlayerControl;
            if (onItemClickLisenter != null) {
                onItemClickLisenter.showPlayProgress(0, playerControl);
            }
        }

        @Override
        public void show() {

        }

        @Override
        public void show(int i) {

        }

        @Override
        public void hide() {

        }

        @Override
        public boolean isShowing() {
            return false;
        }

        @Override
        public void setEnabled(boolean b) {

        }

        @Override
        public void setAnchorView(View view) {

        }
    };

    public void doOpenAnimator(LinearLayout view) {
        final Intent it = new Intent();
        ValueAnimator animator1 = ValueAnimator.ofInt(100, h);
        animator1.setDuration(300);
        final int l = h - 100;
        animator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int curValue = (int) animation.getAnimatedValue();
                int y = 0;
//                y = imgScrollY - (imgScrollY * (curValue - imgHeight) / l);
                view.getLayoutParams().height = curValue;
                view.getLayoutParams().width = curValue * w / h;
//                imageView1.setScrollY(y);
                view.requestLayout();


            }
        });
        animator1.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                it.setClass(mContext, PlayActivity.class);
                mContext.startActivity(it);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animator1.start();


    }

    public interface OnItemClickLisenter {
        void zan(int type, int position, OpusBean opusBean);

        void share(int position, OpusBean opusBean);

        void join(int position, OpusBean opusBean);

        void guanzhu(int position, OpusBean opusBean);

        void comm(int position, OpusBean opusBean);

        void awkward(int position, OpusBean opusBean);

        void longPress(int position, OpusBean opusBean);

        void showPlayProgress(int position, IMediaController.MediaPlayerControl mediaPlayerControl);

        void playSuccess(int position);
        void remmend(int position,OpusBean opusBean);
        void checkAndPass(int position,OpusBean opusBean);
        void startPlay();
    }


    public class FooterHolder extends RecyclerView.ViewHolder {
        View mLoadingViewstubstub;
        View mEndViewstub;
        public OpusAdapterNew.FooterState status = OpusAdapterNew.FooterState.Normal;

        public FooterHolder(View itemView) {
            super(itemView);
            mLoadingViewstubstub = itemView.findViewById(R.id.glistview_footer_content_rel);
            mEndViewstub = itemView.findViewById(R.id.glistview_footer_nomore_rel);
        }

        //根据传过来的status控制哪个状态可见
        public void setStatus(OpusAdapterNew.FooterState status) {
            this.status = status;
            switch (status) {
                case Normal:
                    setAllGone();
                    break;
                case Loading:
                    setAllGone();
                    mLoadingViewstubstub.setVisibility(View.VISIBLE);
                    break;
                case TheEnd:
                    setAllGone();
                    mEndViewstub.setVisibility(View.VISIBLE);
                    break;
                default:
                    break;
            }

        }

        //全部不可见
        void setAllGone() {
            if (mLoadingViewstubstub != null) {
                mLoadingViewstubstub.setVisibility(View.GONE);
            }
            if (mEndViewstub != null) {
                mEndViewstub.setVisibility(View.GONE);
            }
        }

    }

    public void setSharkFlag(boolean sharkFlag) {
        this.sharkFlag = sharkFlag;
    }

    public void stopPlay(int position) {
        if (videoPlayer.get(position + "") != null) {
            videoPlayer.get(position + "").videoMian.stopPlayback();
        }
        try {
            Set<Map.Entry<String, ViewHolder>> set = videoPlayer.entrySet();
            for (Map.Entry<String, ViewHolder> me : set) {
                ViewHolder value = me.getValue();
                if (value != null) {
                    value.videoMian.stopPlayback();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void startPlay(int position) {
        if (videoPlayer.get(position + "") != null) {
            final OpusBean opusBean = videoList.get(position);
            mTrand = opusBean.getTranspondCount();
            ViewHolder viewHolder = videoPlayer.get(position + "");
            if (opusBean.getWidth() == 0 || opusBean.getHeight() == 0) {
                viewHolder.videoMian.setDisplayAspectRatio(PLVideoTextureView.ASPECT_RATIO_PAVED_PARENT);//铺满屏幕
                ImageUtils.LoadImagePlay(mContext,opusBean.getVideoCverUrl(),videoPlayer.get(position + "").video_cover);
                viewHolder.videoMian.setCoverView(viewHolder.video_cover);
            } else if (opusBean.getWidth() > opusBean.getHeight()) {//宽度大于高度
                viewHolder.videoMian.setDisplayAspectRatio(PLVideoTextureView.ASPECT_RATIO_FIT_PARENT);//适应屏幕
                ImageUtils.LoadImagePlay(mContext,"",videoPlayer.get(position + "").video_cover);
                viewHolder.videoMian.setCoverView(viewHolder.video_cover);
            } else {
                viewHolder.videoMian.setDisplayAspectRatio(PLVideoTextureView.ASPECT_RATIO_PAVED_PARENT);//铺满屏幕
                ImageUtils.LoadImagePlay(mContext,opusBean.getVideoCverUrl(),videoPlayer.get(position + "").video_cover);
                viewHolder.videoMian.setCoverView(viewHolder.video_cover);
            }
            if (!viewHolder.videoMian.isPlaying()) {
                viewHolder.video_cover.setFitsSystemWindows(true);
//                Glide.with(mContext).load(opusBean.getVideoCverUrl()).into(videoPlayer.get(position + "").video_cover);
//                videoPlayer.get(position + "").videoMian.setCoverView(videoPlayer.get(position + "").video_cover);
                viewHolder.videoMian.setVideoPath(opusBean.getVideoUrl());
                viewHolder.videoMian.start();
                viewHolder.img_play.setVisibility(View.INVISIBLE);
                viewHolder.likeView.setText(StringUtils.formatZanCount(opusBean.getLikeCount()));
                viewHolder.tv_comment.setText(StringUtils.formatZanCount(opusBean.getCommentCount()));
                viewHolder.tv_share.setText(StringUtils.formatZanCount(mTrand));
                if (0 != opusBean.getCostarCount()) {
                    viewHolder.mCountTv.setVisibility(View.VISIBLE);
                    viewHolder.mCountTv.setText(String.valueOf(opusBean.getCostarCount()));
                } else {
                    viewHolder.mCountTv.setVisibility(View.INVISIBLE);
                }
            }
            AppListener.getInstance().setCommentTotalListener(new AppListener.OnCommentTotalListener() {
                @Override
                public void onCommonet(String total) {
                    viewHolder.tv_comment.setText(total);
                }
            });
            AppListener.getInstance().setOnShareTotalListener(new AppListener.OnShareTotalListener() {
                @Override
                public void onShareTotal(String shareTotal) {
                    mTrand = mTrand +1;
                    viewHolder.tv_share.setText(StringUtils.formatZanCount(mTrand));
                }
            });
            AppListener.getInstance().setOnFocusBackListener(new AppListener.OnFocusBackListener() {
                @Override
                public void onFocusBack(String focus) {
                    String[] mProfileId = focus.split("::");
                    if (mProfileId[0].equals(String.valueOf(opusBean.getProfileId()))) {
                        if ("1".equals(mProfileId[1])) {
                            videoPlayer.get(position + "").add_img.setVisibility(View.GONE);
                        } else if ("2".equals(mProfileId[1])) {
                            videoPlayer.get(position + "").add_img.setVisibility(View.VISIBLE);
                        }
                    }
                }
            });
        }
    }

    public void clearPlayer() {
        if (videoPlayer != null) {
            try {
                Set<Map.Entry<String, ViewHolder>> set = videoPlayer.entrySet();
                for (Map.Entry<String, ViewHolder> me : set) {
                    ViewHolder value = me.getValue();
                    if (value != null) {
                        value.videoMian.stopPlayback();
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            videoPlayer.clear();
            videoIds.clear();
        }
    }

    private void addPlayer(int position, ViewHolder holder) {
        if (videoPlayer == null) {
            videoPlayer = new HashMap<>();
        }
        if (videoIds.size() < 5) {
            videoPlayer.put(position + "", holder);
            videoIds.add(position + "");
        } else {
            String id = videoIds.remove(0);
            if (videoPlayer.get(id + "") != null) {
                videoPlayer.get(id + "").videoMian.stopPlayback();
            }
            videoPlayer.remove(id);
            videoPlayer.put(position + "", holder);
            videoIds.add(position + "");
        }

    }

    public void setActivityListener(ActivityClickListener mListener) {
        this.mActivityClickListener = mListener;
    }

    public interface ActivityClickListener {
        void ActivityClick(OpusBean opusBean);
    }
}
